<template>
  <div class="main">
    <!-- 顶部导航栏组件 -->
    <van-nav-bar title="购物车" @click-left="onClickLeft">
      <template #left>
        <img
          src=""
          class="nav-arrow"
        />
      </template>
    </van-nav-bar>
    <div class="no-goods" v-if="isNoGoods">
      <img
        src="https://img11.360buyimg.com/jdphoto/s180x180_jfs/t18163/292/540553659/74408/adeb7463/5a93c51cN3bb5e37b.png"
        alt=""
      />
      <p>购物车空空如也，去逛逛吧~</p>
    </div>
    <div class="goods-cart">
      <div class="goodlist">
        <div
          class="good-item"
          v-for="(item, index) in goodslist"
          :key="item.id"
        >
          <input
            type="checkbox"
            class="item-checkbox"
            v-model="goodsChecked"
            :value="item"
          />
          <van-card>
            <template #price>
              <span class="price-wrap"
                >¥ <span class="good-price"> {{ goodPrice }}</span></span
              >
            </template>
            <template #title>
              <span class="good-tit">{{ item.productItem }}</span>
            </template>
            <template #thumb>
              <img :src="item.href" alt="" />
            </template>
            <template #num>
              <button class="reduce" @click="reduceGood(index)">-</button>
              <span class="goodnum">{{ item.goodnum }}</span>
              <button class="add" @click="item.goodnum++">+</button>
            </template>
          </van-card>
        </div>
      </div>
    </div>
    <div class="footer">
      <label class="fl">
        <input
          type="checkbox"
          class="checkAll"
          v-model="checkAll"
          @change="changeAll"
        />
        全选
      </label>
      <button :class="{ 'go-buy': true, fr: true, disabled: isDisabled }">
        支付
      </button>
      <p class="fr price-container">
        合计：<span class="price-wrap"
          >¥<span class="total-price">{{ totalPrice }}</span></span
        >
      </p>
    </div>
  </div>
</template>

<script>
import Vue from "vue";
import { Card } from "vant";
import axios from "axios";
Vue.use(Card);

export default {
  data() {
    return {
      isNoGoods: false,
      goodsChecked: [],
      goodslist: [],
      goodPrice: 99.99,
      checkAll: false,
      isDisabled: false,
    };
  },
  methods: {
    // 返回上一页
    onClickLeft() {
      this.$router.go(-1);
    },
    reduceGood(index) {
      if (this.goodslist[index].goodnum > 1) {
        this.goodslist[index].goodnum--;
      }
    },
    changeAll() {
      if (this.checkAll) {
        this.goodsChecked = this.goodslist;
      } else {
        this.goodsChecked = [];
      }
    },
  },
  computed: {
    totalPrice() {
      let total = 0;
      this.goodsChecked.map((item) => {
        total += item.goodnum * this.goodPrice;
      });
      // 根据 total 判断是否给支付按钮添加禁用样式
      if (total <= 0) {
        this.isDisabled = true;
      } else {
        this.isDisabled = false;
      }
      return total.toFixed(2);
    },
  },
  watch: {
    goodsChecked: function () {
      this.checkAll = this.goodsChecked.length === this.goodslist.length;
    },
  },
  created() {
    this.$store.commit("setIsShow", false);
    // 获取数据
    axios
      .get(
        "https://www.fastmock.site/mock/b2f584f37a59c340f74cfbd834be25ff/fenlei1/all"
      )
      .then((res) => {
        res = res.data;
        console.log(this.$route);
        let id = "00018";
        for (var i = 0; i < res.length; i++) {
          if (res[i].id === id) {
            let arr = res.splice(i, 4);
            let num = 1;
            for (let k = 0; k < arr.length; k++) {
              num++;
              arr[k].goodnum = num;
            }
            this.goodslist = arr;
          }
        }
      });
  },
  beforeDestroy() {
    this.$store.commit("setIsShow", true);
  },
};
</script>

<style scoped>
html,
body {
  height: 100%;
}
.main {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #f7f7f7;
}
.nav-arrow {
  width: 24px;
}
.van-nav-bar__left {
  padding-left: 25px;
}
.van-nav-bar__title {
  font-size: 17px;
}
.no-goods {
  padding: 70px 0 65px;
  text-align: center;
}
.no-goods img {
  width: 90px;
  height: 90px;
}
.no-goods p {
  margin-top: 15px;
  font-size: 16px;
  color: #333;
}
.header {
  line-height: 44px;
  font-size: 16px;
  text-align: center;
  background-color: #fff;
}
.good-item {
  display: flex;
  background-color: #fff;
  margin: 10px;
  padding: 10px 15px 18px 0;
  margin-top: 10px;
  border-radius: 8px;
  align-items: center;
}
.van-card__thumb {
  width: 80px;
  height: 80px;
}
.van-card__thumb img {
  width: 80px;
}
.good-tit {
  font-size: 18px;
  color: #000;
}
.item-checkbox {
  margin: 0 15px;
}
.van-card {
  background-color: #fff;
  padding: 0;
}
.reduce,
.add {
  width: 20px;
  height: 20px;
  background-color: #f8f9fa;
  border: none;
  outline: none;
  color: #333;
}
.goodnum {
  display: inline-block;
  width: 20px;
  line-height: 20px;
  text-align: center;
  background-color: #f2f3f5;
  margin: 0 2px;
  color: #111;
}
.fl {
  float: left;
}
.fr {
  float: right;
}
.footer {
  box-sizing: border-box;
  width: 100%;
  position: fixed;
  bottom: 0;
  padding: 0 15px;
  height: 50px;
  line-height: 50px;
  background-color: #fff;
}
.go-buy {
  margin-top: 5px;
  margin-left: 10px;
  width: 110px;
  line-height: 40px;
  border-radius: 20px;
  border: 0;
  outline: none;
  background: linear-gradient(90deg, #ff6034, #ee0a24);
  color: #fff;
}
.disabled {
  opacity: 0.5;
}
.price-wrap {
  color: #ee0a24;
  font-size: 14px;
  font-weight: 700;
}
.good-price {
  font-size: 18px;
}
.total-price {
  font-size: 18px;
  font-weight: 700;
}
.price-container {
  width: 150px;
  text-align: right;
}
.van-card {
  width: 100%;
}
</style>